package m.jclecai.com.adapter;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import m.jclecai.com.R;

import m.jclecai.com.adapter.holder.JclqChildHolder;
import m.jclecai.com.adapter.holder.JclqGroupHolder;
import m.jclecai.com.adapter.holder.ViewHolder;
import m.jclecai.com.bean.LotteryGroupBean;
import m.jclecai.com.bean.ZqItem;
import m.jclecai.com.listener.NotifyTextViewSetChanged;
import m.jclecai.com.utils.BaseUtils;
import m.jclecai.com.utils.Utils;
import m.jclecai.com.view.CustomExpandListview;

import static m.jclecai.com.utils.Utils.ZQ_ITEM_LIST;
import static m.jclecai.com.utils.Utils.jclqGroup;
import static m.jclecai.com.utils.Utils.zq_jczqChilds;

/**
 * Created by zd on 2017/5/3.
 */

public class ExpandJCLQAdapter extends BaseExpandableListViewAdapter2<ViewHolder> implements CustomExpandListview.HeaderAdapter {

    Context mContext;
    int pager = 0;
    NotifyTextViewSetChanged change;
    CustomExpandListview listview;

    public ExpandJCLQAdapter(Context context, int pager, NotifyTextViewSetChanged change, CustomExpandListview listview) {
        this.mContext = context;
        this.pager = pager;
        this.change = change;
        this.listview = listview;
    }

    public void setPager(int pager) {
        this.pager = pager;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateChildViewHolder(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        return new JclqChildHolder(this.mContext, R.layout.item_lq);
    }

    @Override
    public void onBindChildViewHolder(int groupPosition, int childPosition, boolean isLastChild, ViewHolder viewHolder) {
        if (zq_jczqChilds.size() <= 0) return;
        JclqChildHolder vh = (JclqChildHolder) viewHolder;
        ZqItem item = zq_jczqChilds.get(groupPosition).get(childPosition);
        vh.init(item, pager, groupPosition * getRealChildrenCount(groupPosition) + childPosition);
        vh.setChangeListener(change);
    }

    @Override
    public ViewHolder onCreateParentViewHolder(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        return new JclqGroupHolder(this.mContext, R.layout.item_parent);
    }

    @Override
    public void onBindParentViewHolder(int groupPosition, boolean isExpanded, ViewHolder viewHolder) {
        JclqGroupHolder vh = (JclqGroupHolder) viewHolder;
        vh.init(jclqGroup, groupPosition, getRealChildrenCount(groupPosition), isExpanded, pager);
    }

    @Override
    public int getGroupCount() {
        return jclqGroup.size();
    }

//    @Override
//    public int getChildrenCount(int groupPosition) {
//        return groupPosition < 0 ? 0 : Utils.zq_jczqChilds.size() <= 0 ? 0 : Utils.zq_jczqChilds.get(groupPosition).size();
//    }

    @Override
    public int getRealChildrenCount(int groupPosition) {
        return groupPosition < 0 ? 0 : Utils.zq_jczqChilds.size() <= 0 ? 0 : Utils.zq_jczqChilds.get(groupPosition).size();
    }

    //重置勾选状态
    public void resetStatus() {
        for (int j = 0; j < zq_jczqChilds.size(); j++) {
            for (int k = 0; k < zq_jczqChilds.get(j).size(); k++) {
                if (ZQ_ITEM_LIST.size() > 0 && ZQ_ITEM_LIST.contains(zq_jczqChilds.get(j).get(k)))
                    continue;
                zq_jczqChilds.get(j).get(k).setMap(null);
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public int getHeaderState(int groupPosition, int childPosition) {
        final int childCount = getChildrenCount(groupPosition);
        if (childPosition == childCount - 1) {
            return PINNED_HEADER_PUSHED_UP;
        } else if (childPosition == -1
                && !listview.isGroupExpanded(groupPosition)) {
            return PINNED_HEADER_GONE;
        } else {
            return PINNED_HEADER_VISIBLE;
        }
    }

    @Override
    public void configureHeader(View header, int groupPosition, int childPosition, int alpha) {
        if (groupPosition > -1) {
            LotteryGroupBean bean = jclqGroup.get(groupPosition);
            TextView list_item_parent_title = (TextView) header.findViewById(R.id.list_item_parent_title);
            list_item_parent_title.setText(jclqGroup.get(groupPosition).getTitle()
                    + "  " + BaseUtils.dateToWeek(jclqGroup.get(groupPosition).getTitle())
                    + "  " + getRealChildrenCount(groupPosition) + "场可投");
            //支持单关的小蓝点
            header.findViewById(R.id.tv_dg).setVisibility(bean.isLqDG(pager) ? View.INVISIBLE : View.INVISIBLE);
        }
    }
}
